﻿using Mysoft.Common.Configuration;
using Mysoft.Common.Const;
using Mysoft.Common.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Mysoft.Common.Extensions
{
    /// <summary>
    /// GUID扩展
    /// </summary>
    public static class GuidExtension
    {
        private const byte version = (byte)4;
        private const byte variant = (byte)8;
        private const byte filterHighBit = 0b00001111;
        private const byte filterLowBit = 0b11110000;
        private static readonly RandomNumberGenerator _randomNumberGenerator = RandomNumberGenerator.Create();

        /// <summary>
        /// 连续 Guid 类型，默认：AsString.
        /// </summary>
        private static SequentialGuidType SequentialGuidType {
            get;
            set;
        }
      

        /// <summary>
        /// 生成有序 Guid.
        /// </summary>
        /// <returns></returns>
        public static System.Guid NewSeqGuid(this System.Guid guid)
        {
            switch (AppSetting.DBType)
            {
                case DBType.MsSql:
                    SequentialGuidType = SequentialGuidType.AtEnd;
                    break;
                case DBType.MySql:
                case DBType.PgSql:
                    SequentialGuidType = SequentialGuidType.AsString;
                    break;
                default:
                    SequentialGuidType = SequentialGuidType.AsBinary;
                    break;
            }

            return Next(SequentialGuidType);
        }

        /// <summary>
        /// 生成连续 Guid.
        /// </summary>
        /// <param name="guidType"></param>
        /// <returns></returns>
        private static System.Guid Next(SequentialGuidType guidType)
        {
            // see: What is a GUID? http://guid.one/guid
            // see: https://github.com/richardtallent/RT.Comb#gory-details-about-uuids-and-guids
            // According to RFC 4122:
            // dddddddd-dddd-Mddd-Ndrr-rrrrrrrrrrrr
            // - M = RFC 版本（version）, 版本4的话，值为4
            // - N = RFC 变体（variant），值为 8, 9, A, B 其中一个，这里固定为8
            // - d = 从公元1年1月1日0时至今的时钟周期数（DateTime.UtcNow.Ticks）
            // - r = 随机数（random bytes）

            var randomBytes = new byte[8];
            _randomNumberGenerator.GetBytes(randomBytes);

            long timestamp = DateTime.UtcNow.Ticks;

            byte[] timestampBytes = BitConverter.GetBytes(timestamp);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(timestampBytes);
            }

            byte[] guidBytes = new byte[16];

            switch (guidType)
            {
                case SequentialGuidType.AsString:
                case SequentialGuidType.AsBinary:

                    // AsString: dddddddd-dddd-Mddd-Ndrr-rrrrrrrrrrrr
                    Buffer.BlockCopy(timestampBytes, 0, guidBytes, 0, 6); // 时间戳前6个字节，共48位
                    guidBytes[6] = (byte)((version << 4) | ((timestampBytes[6] & filterLowBit) >> 4)); // 高4位为版本 | 低4位取时间戳序号[6]的元素的高4位
                    guidBytes[7] = (byte)(((timestampBytes[6] & filterHighBit) << 4) | ((timestampBytes[7] & filterLowBit) >> 4)); // 高4位取：[6]低4位 | 低4位取：[7]高4位
                    guidBytes[8] = (byte)((variant << 4) | (timestampBytes[7] & filterHighBit)); // 高4位为：变体 | 低4位取：[7]低4位
                    Buffer.BlockCopy(randomBytes, 0, guidBytes, 9, 7); // 余下7个字节由随机数组填充

                    // .NET中，Data1、Data2、Data3 块 分别视为 Int32、Int16、Int16，在小端系统，需要翻转这3个块。
                    if (guidType == SequentialGuidType.AsString && BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(guidBytes, 0, 4);
                        Array.Reverse(guidBytes, 4, 2);
                        Array.Reverse(guidBytes, 6, 2);
                    }

                    break;

                case SequentialGuidType.AtEnd:

                    // AtEnd: rrrrrrrr-rrrr-Mxdr-Nddd-dddddddddddd
                    // Block: 1        2    3    4    5
                    // Data4 = Block4 + Block5
                    // 排序顺序：Block5 > Block4 > Block3 > Block2 > Block1
                    // Data3 = Block3 被认为是 uint16，排序并不是从左到右，为消除影响，x 位取固定值

                    Buffer.BlockCopy(randomBytes, 0, guidBytes, 0, 6);
                    guidBytes[6] = (byte)(version << 4); // Mx 高4位为版本 | 低4位取：全0
                    guidBytes[7] = (byte)(((timestampBytes[7] & filterHighBit) << 4) | (randomBytes[7] & filterHighBit)); ; // dr 高4位为：时间戳[7]低4位 | 低4位取：随机数
                    guidBytes[8] = (byte)((variant << 4) | ((timestampBytes[6] & filterLowBit) >> 4)); // Nd 高4位为：变体 | 低4位取：时间戳[6]高4位
                    guidBytes[9] = (byte)(((timestampBytes[6] & filterHighBit) << 4) | ((timestampBytes[7] & filterLowBit) >> 4)); // dd 高4位为：时间戳[6]低4位 | 低4位取：时间戳[7]高4位
                    Buffer.BlockCopy(timestampBytes, 0, guidBytes, 10, 6); // 时间戳前6个字节

                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(guidBytes, 6, 2); // 包含版本号的 Data3 块需要翻转
                    }
                    break;
            }

            return new System.Guid(guidBytes);
        }
    }
  
}
